home *** CD-ROM | disk | FTP | other *** search
- Path: ix.netcom.com!news
- From: miker3@ix.netcom.com (Mike Rubenstein)
- Newsgroups: comp.lang.c++
- Subject: Re: extern consts
- Date: Mon, 18 Mar 1996 12:49:47 GMT
- Organization: Netcom
- Message-ID: <314d5987.219100870@nntp.ix.netcom.com>
- References: <4idbcv$ue2@news7.erols.com> <314c2077.138956468@nntp.ix.netcom.com> <4ii7h9$bq6@news6.erols.com>
- NNTP-Posting-Host: ix-dc6-01.ix.netcom.com
- X-NETCOM-Date: Mon Mar 18 6:48:19 AM CST 1996
- X-Newsreader: Forte Agent .99d/32.182
-
- Chris Cobb <ccobb@cseg.com> wrote:
-
- > miker3@ix.netcom.com (Mike Rubenstein) wrote:
- > >
- > >It's not legal. extern const is legal, but in any compilation unit
- > >that does not see the initializer the const variable is not a constant
- > >expression.
- > >
- >
- > Well, your comment makes sense. However, in a way it seem
- > self-contradictory. If a const can be extern, then it no longer is a
- > const: you've basically externed away constness. But if thats what
- > externinig a const does, it doesn't make sense to allow it at all (which
- > is what happens on most compilers.)
-
- Others have already given some good answers, but I'll add that an
- extern const (without an initializer) has the same semantics as a
- const in C. In C a const variable is never a constant expression. In
- C++ it is a constant expression as long as it is initialized with a
- constant expression and the initialization is seen in the compilation
- unit.
-
- In C and C++ a const is a VARIABLE that may not be changed. It may be
- used anywhere a variable is permitted as long as it is not changed.
-
- In C++, but not C, a const that is initialized with a constant
- expression may also be used whereever a constant expression is
- permitted. This adds tremendously to the usefulness of the concept,
- but there still is some use for const variables.
-
- Note that there are other things than being extern that can prevent a
- const from being used as a constant expression. For example,
-
- int foo();
- const int bar = foo();
-
- is legal (in C it is legal only in function scope -- in C++ it is
- legal elsewhere). However, bar cannot be used where a constant
- expression is required since it is not initialized with a constant
- expression.
-
- Michael M Rubenstein
-